using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using IGlobalist.Cms.Common;


namespace IGlobalist.Cms.Admin.Control
{
    public partial class SettingAddEdit : AdminBaseUserControl
    {
        private XmlDocument xDoc=null;
        string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/SiteConfig.config");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                #region cofig metatag
                    if (Session["Cofig"] == null)
                    {
                        xDoc = new XmlDocument();
                        xDoc.Load(path);
                        Session["Cofig"] = xDoc;
                    }
                    else
                        xDoc = (XmlDocument)Session["Cofig"];
                    BindData();
                #endregion
                BindPayment();
            }
        }

        public void DoSave(object sender, EventArgs e)
        {
            
        }

        public void BindData()
        {
            if (xDoc != null)
            {
                string xpath = String.Format("//Setting[@key='{0}']", "MetaDescription");
                XmlNode xNode = xDoc.DocumentElement.SelectSingleNode(xpath);
                tbDescription.Text = xNode.Attributes["value"].Value;

                xpath = String.Format("//Setting[@key='{0}']", "MetaKeyWords");
                xNode = xDoc.DocumentElement.SelectSingleNode(xpath);
                tbKeyWords.Text = xNode.Attributes["value"].Value;
            }
        }

        public void BindPayment()
        {
            PaymentInfo payment = PaymentPro.GetProfileGogleCheckOut();
            tbGoogleEnvironment.Text = payment.GoogleEnvironment;
            tbGoogleMerchantID.Text = payment.GoogleMerchantID;
            tbGoogleMerchantKey.Text = payment.GoogleMerchantKey;
            tbRootDataFolder.Text = payment.RootDataFolder;

            payment = PaymentPro.GetProfilePaypalInfo();
            tbPassword.Text = payment.APIPassword;
            tbUsername.Text = payment.APIUsername;
            tbEnviroment.Text = payment.Environment;
            tbSignature.Text = payment.Signature;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region meta tag
                if (Session["Cofig"] == null)
                {
                    xDoc = new XmlDocument();
                    xDoc.Load(path);
                    Session["Cofig"] = xDoc;
                }
                else
                    xDoc = (XmlDocument)Session["Cofig"];

                string xpath = String.Format("//Setting[@key='{0}']", "MetaDescription");
                XmlNode xNode = xDoc.DocumentElement.SelectSingleNode(xpath);
                xNode.Attributes["value"].Value = tbDescription.Text;

                xpath = String.Format("//Setting[@key='{0}']", "MetaKeyWords");
                xNode = xDoc.DocumentElement.SelectSingleNode(xpath);
                xNode.Attributes["value"].Value = tbKeyWords.Text;

                xDoc.Save(path);
            #endregion

            #region payment
            PaymentInfo payment=new PaymentInfo();
            payment.GoogleEnvironment = tbGoogleEnvironment.Text;
            payment.GoogleMerchantID=tbGoogleMerchantID.Text ;
            payment.GoogleMerchantKey=tbGoogleMerchantKey.Text;
            payment.RootDataFolder=tbRootDataFolder.Text;
            PaymentPro.UpdateMerchantAccount(payment);

            payment.APIPassword=tbPassword.Text;
            payment.APIUsername=tbUsername.Text;
            payment.Environment=tbEnviroment.Text;
            payment.Signature=tbSignature.Text;
            PaymentPro.UpdateMerchantAccount(payment);

            #endregion
         }
    }
}